package constant.milk.cashbook.sms.parser;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import constant.milk.cashbook.bean.SmsInputData;
import constant.milk.cashbook.sms.SmsNameData;

public class SmsParser 
{
	// 문자 입금인지 출금인지 할부인지
	public static final int OUT_SMS = 0; // 지출
	public static final int IN_SMS = 1; // 수입
	public static final int OUT_ATM_SMS = 2; // 현금출금
	public static final int IN_ATM_SMS = 3; // 현금입금
	public static final int HALBU_SMS = 4; // 할부
	
	private int smsType; 
	private boolean flagATM = false;
	private boolean flagHalbu = false;
	private String halbuMonth = "0";
	
	private boolean FLAG = true;
	private boolean available = false;
	
	private boolean outmoneyFlag = false;
	
	//1:check 2:credit
	private int checkcreditFlag = 0;
	
	private final int CHECK = 1;
	private final int CREDIT = 2;
	
	private String message;
	
	private String date;
	
	private SmsInputData data;
	
	public SmsParser(String message) 
	{
		this.message = message;
	}
	
	public SmsInputData messageParsing() 
	{
		try {
			splitMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		return data;
	}
	
	public void setDate(String date)
	{
		this.date = date;
	}
	
	public boolean getAvailable() 
	{
		return available;
	}
	
	public int getSmsType() 
	{
		return smsType;
	}
	
	public boolean getFlagATM() 
	{
		return flagATM;
	}
	
	public boolean getFlagHalbu() 
	{
		return flagHalbu;
	}
	
	public String getHalbuMonth() 
	{
		return halbuMonth;
	}
	
	private void splitMessage() 
	{
		FLAG = true;
		
		String message = "";
		
		Pattern outPattern1 = Pattern.compile("일시불");	// 체크+신용에서 신용
		Matcher outMatcher1 = outPattern1.matcher(this.message);
		
		Pattern outPattern2 = Pattern.compile("승인");
		Matcher outMatcher2 = outPattern2.matcher(this.message);
		
		Pattern outPattern3 = Pattern.compile("체크승인");	// 농협체크+신용에서 체크/신한체크+신용에서 체크
		Matcher outMatcher3 = outPattern3.matcher(this.message);
		
		Pattern outPattern4 = Pattern.compile("체크신용");	// 국민체크+신용에서 신용/신한체크+신용에서 신용
		Matcher outMatcher4 = outPattern4.matcher(this.message);
		
		Pattern outPattern5 = Pattern.compile("국민체크");	// 국민체크+신용에서 체크
		Matcher outMatcher5 = outPattern5.matcher(this.message);
		
		Pattern outPattern6 = Pattern.compile("체크결제");	// 외환체크+신용에서 체크
		Matcher outMatcher6 = outPattern6.matcher(this.message);
		
		
		if (outMatcher1.find() || outMatcher2.find() || outMatcher4.find())
		{
			outmoneyFlag = true;
			checkcreditFlag = CREDIT;
		}
		
		if (outMatcher3.find() || outMatcher5.find() || outMatcher6.find())
		{
			checkcreditFlag = CHECK;
		}
		
		this.message = this.message.replace("(일시불)", " ")
								.replaceAll("\\(초과승인\\d*,*\\d*,*\\d*,*\\d*,*\\d원\\)", " ")
								.replace("[일시불.승인]", " ").replace("[체크.승인]", " ")
								.replace("[체크승인]", " ").replace("일시불", " ")
								.replace("정상승인", " ")
								.replace("승인내역", " ")
								.replace("-승인", " ")
								.replace("승인", " ");
		
		message = this.message;
		
		Pattern pCancel = Pattern.compile("취소");
		Matcher mCancel = pCancel.matcher(message);
		
		Pattern pCancel2 = Pattern.compile("결제예정");
		Matcher mCancel2 = pCancel2.matcher(message);
		
		Pattern pCancel6 = Pattern.compile("\\d개월");
		Matcher mCancel6 = pCancel6.matcher(message);
		
		Pattern pCancel7 = Pattern.compile("할부");
		Matcher mCancel7 = pCancel7.matcher(message);
		
		if (mCancel.find() || mCancel2.find())
		{
			FLAG = false;
		}
		
		if (FLAG) 
		{
			ArrayList<String> cardName = new ArrayList<String>();
			cardName = new SmsNameData().getCardName();
			
			data = new SmsInputData();
			
			for (int i=0; i<cardName.size(); i++) 
			{
				Pattern pName = Pattern.compile("[0-9\\W]*(" + cardName.get(i) + "[^\\W]*)\\W");
				
				Matcher mName = pName.matcher(message.substring(0, 25));
				
				if (mName.find()) 
				{
					for (int j=0; j<mName.groupCount(); j++) 
					{
						data.setName(mName.group(1));
						available = true;
					}
					
					break;
				}
			}
			
			if (!available)
			{
				for (int i=0; i<cardName.size(); i++) 
				{
					Pattern pName = Pattern.compile("[0-9\\W]*("+cardName.get(i)+"[^\\W]*)\\W*");
					
					Matcher mName = pName.matcher(message.substring(message.length()-20, message.length()));
					
					if (mName.find()) 
					{
						for (int j=0; j<mName.groupCount(); j++) 
						{
							data.setName(mName.group(1));
							available = true;
						}
						break;
					}
				}
			}
			
			//패턴에 없는 할부일때 처리해야함
			Pattern pType = Pattern.compile("입금");
			Matcher mType = pType.matcher(message);
			
			Pattern pType2 = Pattern.compile("계좌충전");
			Matcher mType2 = pType2.matcher(message);
			
			Pattern pType3 = Pattern.compile("ATM");
			Matcher mType3 = pType3.matcher(message);
			
			Pattern pType4 = Pattern.compile("할부");
			Matcher mType4 = pType4.matcher(message);
			
			Pattern pType5 = Pattern.compile("전자금융급여");
			Matcher mType5 = pType5.matcher(message);
			
			Pattern pType6 = Pattern.compile("포인트지급");
			Matcher mType6 = pType6.matcher(message);
			
			Pattern pType7 = Pattern.compile("(\\d{1,2})개월");
			Matcher mType7 = pType7.matcher(message);
			
			Pattern pType8 = Pattern.compile("출금정정");
			Matcher mType8 = pType8.matcher(message);
			
			Pattern pType9 = Pattern.compile("\\d원\\((\\d{1,2})월\\)");
			Matcher mType9 = pType9.matcher(message);
			
			Pattern pType10 = Pattern.compile("\\]입\\s+\\d*,*\\d*,*\\d*,*\\d\\s*잔");
			Matcher mType10 = pType10.matcher(message);
			
			if ( mType3.find() )
			{
				flagATM = true;
			}
			
			if ( mType4.find() )
			{
				flagHalbu = true;
				
				if (mType7.find())
				{
					halbuMonth = mType7.group(1);
				}
			}
			else if ( mType7.find() )
			{
				flagHalbu = true;
				halbuMonth = mType7.group(1);
			}
			else if ( mType9.find() )
			{
				flagHalbu = true;
				halbuMonth = mType9.group(1);
			}
			
			
			if (data.getName() != null)
			{
				if (flagHalbu)
				{
					smsType = HALBU_SMS;
					//halbuMoney();
					SmsHalbuMoney som = new SmsHalbuMoney(data, message, date, smsType, checkcreditFlag);
					data = som.getData();
					smsType = som.getSmsType();
					//halbuMonth = som.getHalbuMonth();
					available = som.getAvailable();
				}
				else if ( !outmoneyFlag && 
						( mType8.find() || mType.find() || mType5.find() || mType6.find() || mType2.find() || mType10.find() || ( data.getName() != null && (data.getName().equalsIgnoreCase("올레BeF"))))
					) 
								
				{
					if (flagATM)
					{
						smsType = IN_ATM_SMS;
					}
					else
					{
						smsType = IN_SMS;
					}
					
					//inMoney();
					SmsInMoney som = new SmsInMoney(data, message, date, smsType, checkcreditFlag);
					data = som.getData();
					smsType = som.getSmsType();
					available = som.getAvailable();
				}
				else
				{
					if (flagATM)
					{
						smsType = OUT_ATM_SMS;
					}
					else
					{
						smsType = OUT_SMS;
					}
					
					//outMoney();
					SmsOutMoney som = new SmsOutMoney(data, message, date, smsType, checkcreditFlag);
					data = som.getData();
					smsType = som.getSmsType();
					available = som.getAvailable();
				}
				
				if ( (data.getMemo1() == null) || (data.getMemo1().equalsIgnoreCase("")) 
						&& ( (data.getMemo2() == null) || (data.getMemo2().equalsIgnoreCase("")) )
						&& ( (data.getMemo3() == null) || (data.getMemo3().equalsIgnoreCase("")) )
						&& ( (data.getMemo4() == null) || (data.getMemo4().equalsIgnoreCase("")) ) )
				{
					available = false;
				}
			}
			else
			{
				available = false;
			}
			
			if (available == false)
			{
				if (FLAG) 
				{
					ArrayList<String> cardName2 = new ArrayList<String>();
					cardName2 = new SmsNameData().getCardName();
					
					String faultCardName = data.getName();
					
					if (faultCardName == null)
					{
						return;
					}
					
					data = new SmsInputData();
					
					for (int i=0; i<cardName2.size(); i++) {
						//Pattern pName = Pattern.compile("[0-9\\W]*([^0-9\\W]*"+cardName.get(i)+"[^\\W]*)[\\W]*");
						Pattern pName = Pattern.compile("[0-9\\W]*("+cardName2.get(i)+"[^\\W]*)\\W");
						Matcher mName = pName.matcher(message.substring(0, 25));
						if (mName.find()) {
							
							for (int j=0; j<mName.groupCount(); j++) {
								
								if (mName.group(1).equalsIgnoreCase(faultCardName))
								{
									break;
								}
								else
								{
									data.setName(mName.group(1));
//									Log.e("SMSParser", "available true");
									available = true;
								}
							}
							
							if (data.getName() == null || data.getName().equalsIgnoreCase(faultCardName))
							{
								
							}
							else
							{
								if (data.getName() != null && data.getName().length() > 0)
								{
									break;
								}
							}
						}
					}
					
					if ( ! available)
					{
						for (int i=0; i<cardName2.size(); i++) {
							//Pattern pName = Pattern.compile("[0-9\\W]*([^0-9\\W]*"+cardName.get(i)+"[^\\W]*)[\\W]*");
							Pattern pName = Pattern.compile("[0-9\\W]*("+cardName2.get(i)+"[^\\W]*)\\W*");
							Matcher mName = pName.matcher(message.substring(message.length()-20, message.length()));
							if (mName.find()) {
								
								for (int j=0; j<mName.groupCount(); j++) {
									
									if (mName.group(1).equalsIgnoreCase(faultCardName))
									{
										break;
									}
									else
									{
										data.setName(mName.group(1));
//										Log.e("SMSParser", "available true");
										available = true;
									}
								}
								
								if (data.getName() == null || data.getName().equalsIgnoreCase(faultCardName))
								{
									
								}
								else
								{
									if (data.getName() != null && data.getName().length() > 0)
									{
										break;
									}
								}
							}
						}
					}
					
					if (data.getName() != null)
					{
						if ( mType3.find() )
						{
							flagATM = true;
						}
						
						if ( mType4.find() || mType7.find())
						{
							flagHalbu = true;
						}
						
						if (flagHalbu)
						{
							smsType = HALBU_SMS;
							//halbuMoney();
							SmsHalbuMoney som = new SmsHalbuMoney(data, message, date, smsType, checkcreditFlag);
							data = som.getData();
							smsType = som.getSmsType();
							halbuMonth = som.getHalbuMonth();
							available = som.getAvailable();
						}
						else if ( !outmoneyFlag && 
								( mType.find() || mType5.find() || mType6.find() || mType2.find() || ( data.getName() != null && (data.getName().equalsIgnoreCase("올레BeF"))))
							) 
						{
							if (flagATM)
							{
								smsType = IN_ATM_SMS;
							}
							else
							{
								smsType = IN_SMS;
							}
							
							//inMoney();
							SmsInMoney som = new SmsInMoney(data, message, date, smsType, checkcreditFlag);
							data = som.getData();
							smsType = som.getSmsType();
							available = som.getAvailable();
						}
						else
						{
							if (flagATM)
							{
								smsType = OUT_ATM_SMS;
							}
							else
							{
								smsType = OUT_SMS;
							}
							
							//outMoney();
							SmsOutMoney som = new SmsOutMoney(data, message, date, smsType, checkcreditFlag);
							data = som.getData();
							smsType = som.getSmsType();
							available = som.getAvailable();
						}
						
						if ( (data.getMemo1() == null) || (data.getMemo1().equalsIgnoreCase("")) 
								&& ( (data.getMemo2() == null) || (data.getMemo2().equalsIgnoreCase("")) )
								&& ( (data.getMemo3() == null) || (data.getMemo3().equalsIgnoreCase("")) )
								&& ( (data.getMemo4() == null) || (data.getMemo4().equalsIgnoreCase("")) ) )
						{
							available = false;
						}
					}
					else
					{
						available = false;
					}
				}
			}
			
//			Log.e("1", "name : "+data.getName());
//			Log.e("1", "date : "+data.getDate());
//			Log.e("1", "money : "+data.getMoney());
//			Log.e("1", "memo1 : "+data.getMemo1());
//			Log.e("1", "memo2 : "+data.getMemo2());
//			Log.e("1", "memo3 : "+data.getMemo3());
		}
	}
	
}
